package it.univpm.diiga.semanticsearch.gestione;

import it.univpm.diiga.semanticsearch.servizi.Link;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JFormattedTextField;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SpringLayout;
import javax.swing.JButton;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class AddmodrepositoryView extends JDialog {

	private JPanel contentPane;
	private JLabel lbl_title;
	private JLabel lblServiceLink;
	private JLabel lblName;
	private JLabel lblWsdlLink;
	private JLabel lblSwsalLink;
	private JFormattedTextField txtName;
	private JFormattedTextField txtWsdl;
	private JFormattedTextField txtSwsal;
	private JLabel lblBpelLink;
	private JFormattedTextField txtBpel;
	private JFormattedTextField txtServlink;
	private JFormattedTextField txtRepolink;
	private JLabel lblRepositoryLink;
	private JButton btnAddmod;
	private JButton btnInsert;
	
	
	private JLabel lblDescr;
	private JFormattedTextField txtDescr;
	private JLabel lblMeta;
	private JFormattedTextField txtMeta;
	private JButton btnAddMeta;
	private JButton btnDelMeta;
	
	private JScrollPane listPane;
	private JList lstMeta;
	private ListSelectionModel listSelectionModel;
	private DefaultListModel listModel;
	int selectedMeta=-1;
	
	
	private GestioneRepositoryView repoview;
	private int currSel;
	private String mode;
	private JButton btnCancel;

	/**
	 * Create the frame.
	 */
	public AddmodrepositoryView(String selected_mode, Link serv, int index, GestioneRepositoryView rview) {
		
		repoview = rview;		
		currSel = index;
		mode=selected_mode;
		
		
		if(mode.equals("add")){setTitle("Add/Insert new Service");}
		else if(mode.equals("mod")){setTitle("Modify existing Service ("+serv.getServName()+")");}
		
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //Gets screen resolution in a Dimension object
		int winw=350,winh=445;
		setBounds(((screenSize.width/2)-(winw/2)), ((screenSize.height/2)-(winh/2)), 350, 445); //Sets this frame position and size
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout springLayout = new SpringLayout();
		contentPane.setLayout(springLayout);
		{
		
			lbl_title = new JLabel("Input info on new service:");
			if(mode.equals("add")){lbl_title = new JLabel("Input info on new service:");}
			else{lbl_title = new JLabel("Modify service informations:");}
			
			int center_label = ((this.getWidth()/2)-(int)(lbl_title.getMinimumSize().getWidth()/2))-10;
			
			springLayout.putConstraint(SpringLayout.NORTH, lbl_title, 10, SpringLayout.NORTH, contentPane);
			springLayout.putConstraint(SpringLayout.WEST, lbl_title, center_label, SpringLayout.WEST, contentPane);
			contentPane.add(lbl_title);
			
			
		}
		{
			lblName = new JLabel("Name:");
			springLayout.putConstraint(SpringLayout.WEST, lblName, 26, SpringLayout.WEST, contentPane);
			contentPane.add(lblName);
		}
		{
			lblWsdlLink = new JLabel("Wsdl link:");
			springLayout.putConstraint(SpringLayout.NORTH, lblWsdlLink, 95, SpringLayout.NORTH, contentPane);
			springLayout.putConstraint(SpringLayout.SOUTH, lblName, -26, SpringLayout.NORTH, lblWsdlLink);
			springLayout.putConstraint(SpringLayout.WEST, lblWsdlLink, 0, SpringLayout.WEST, lblName);
			contentPane.add(lblWsdlLink);
		}
		{
			lblSwsalLink = new JLabel("Swsal link:");
			springLayout.putConstraint(SpringLayout.NORTH, lblSwsalLink, 6, SpringLayout.SOUTH, lblWsdlLink);
			springLayout.putConstraint(SpringLayout.WEST, lblSwsalLink, 0, SpringLayout.WEST, lblName);
			contentPane.add(lblSwsalLink);
		}
		{
			lblBpelLink = new JLabel("Bpel link:");
			springLayout.putConstraint(SpringLayout.NORTH, lblBpelLink, 6, SpringLayout.SOUTH, lblSwsalLink);
			springLayout.putConstraint(SpringLayout.WEST, lblBpelLink, 0, SpringLayout.WEST, lblName);
			contentPane.add(lblBpelLink);
		}
		{
			lblServiceLink = new JLabel("Service link:");
			springLayout.putConstraint(SpringLayout.WEST, lblServiceLink, 0, SpringLayout.WEST, lblName);
			contentPane.add(lblServiceLink);
		}
		{
			lblRepositoryLink = new JLabel("Repository link:");
			springLayout.putConstraint(SpringLayout.NORTH, lblRepositoryLink, 211, SpringLayout.NORTH, contentPane);
			springLayout.putConstraint(SpringLayout.SOUTH, lblServiceLink, -6, SpringLayout.NORTH, lblRepositoryLink);
			springLayout.putConstraint(SpringLayout.WEST, lblRepositoryLink, 0, SpringLayout.WEST, lblName);
			contentPane.add(lblRepositoryLink);
		}
		
		{
			lblDescr = new JLabel("Description:");
			springLayout.putConstraint(SpringLayout.NORTH, lblDescr, 240, SpringLayout.NORTH, contentPane);
			springLayout.putConstraint(SpringLayout.WEST, lblDescr, 0, SpringLayout.WEST, lblName);
			contentPane.add(lblDescr);
		}
		
	
		{
			lblMeta = new JLabel("Meta-tag:");
			springLayout.putConstraint(SpringLayout.NORTH, lblMeta, 6, SpringLayout.SOUTH, lblDescr);
			springLayout.putConstraint(SpringLayout.WEST, lblMeta, 0, SpringLayout.WEST, lblName);
			contentPane.add(lblMeta);
		}
	
		
		
		{
			txtName = new JFormattedTextField();
			if(mode.equals("mod")){txtName = new JFormattedTextField(serv.getServName());}
			else{txtName = new JFormattedTextField();}
			
			springLayout.putConstraint(SpringLayout.NORTH, txtName, -5, SpringLayout.NORTH, lblName);
			springLayout.putConstraint(SpringLayout.WEST, txtName, 75, SpringLayout.EAST, lblName);
			springLayout.putConstraint(SpringLayout.SOUTH, txtName, 0, SpringLayout.SOUTH, lblName);
			txtName.setColumns(12);
			contentPane.add(txtName);
		}
		{
			txtWsdl = new JFormattedTextField();
			if(mode.equals("mod")&&serv.getWsdlLink()!=null){txtWsdl = new JFormattedTextField(serv.getWsdlLink());}
			else{txtWsdl = new JFormattedTextField();}

			springLayout.putConstraint(SpringLayout.NORTH, txtWsdl, -5, SpringLayout.NORTH, lblWsdlLink);
			springLayout.putConstraint(SpringLayout.WEST, txtWsdl, 0, SpringLayout.WEST, txtName);
			springLayout.putConstraint(SpringLayout.SOUTH, txtWsdl, 0, SpringLayout.SOUTH, lblWsdlLink);
			txtWsdl.setColumns(12);
			contentPane.add(txtWsdl);
		}
		{
			txtSwsal = new JFormattedTextField();
			if(mode.equals("mod")&&serv.getSwsalLink()!=null){txtSwsal = new JFormattedTextField(serv.getSwsalLink());}
			else{txtSwsal = new JFormattedTextField();}

			springLayout.putConstraint(SpringLayout.NORTH, txtSwsal, -5, SpringLayout.NORTH, lblSwsalLink);
			springLayout.putConstraint(SpringLayout.SOUTH, txtSwsal, 0, SpringLayout.SOUTH, lblSwsalLink);
			springLayout.putConstraint(SpringLayout.WEST, txtSwsal, 0, SpringLayout.WEST, txtName);
			txtSwsal.setColumns(12);
			contentPane.add(txtSwsal);
		}
		{
			txtBpel = new JFormattedTextField();
			if(mode.equals("mod")&&serv.getBpellLink()!=null){txtBpel = new JFormattedTextField(serv.getBpellLink());}
			else{txtBpel = new JFormattedTextField();}

			springLayout.putConstraint(SpringLayout.NORTH, txtBpel, -5, SpringLayout.NORTH, lblBpelLink);
			springLayout.putConstraint(SpringLayout.WEST, txtBpel, 0, SpringLayout.WEST, txtName);
			springLayout.putConstraint(SpringLayout.SOUTH, txtBpel, 0, SpringLayout.SOUTH, lblBpelLink);
			txtBpel.setColumns(12);
			contentPane.add(txtBpel);
		}
		{
			txtServlink = new JFormattedTextField();
			if(mode.equals("mod")&&serv.getServLink()!=null){txtServlink = new JFormattedTextField(serv.getServLink());}
			else{txtServlink = new JFormattedTextField();}

			springLayout.putConstraint(SpringLayout.NORTH, txtServlink, -5, SpringLayout.NORTH, lblServiceLink);
			springLayout.putConstraint(SpringLayout.WEST, txtServlink, 0, SpringLayout.WEST, txtName);
			springLayout.putConstraint(SpringLayout.SOUTH, txtServlink, 0, SpringLayout.SOUTH, lblServiceLink);
			txtServlink.setColumns(12);
			contentPane.add(txtServlink);
		}
		{
			txtRepolink = new JFormattedTextField();
			txtRepolink.setEditable(false);
			if(mode.equals("mod")&&serv.getServRepo()!=null){txtRepolink = new JFormattedTextField(serv.getServRepo());}
			else{txtRepolink = new JFormattedTextField();}

			springLayout.putConstraint(SpringLayout.NORTH, txtRepolink, -5, SpringLayout.NORTH, lblRepositoryLink);
			springLayout.putConstraint(SpringLayout.WEST, txtRepolink, 0, SpringLayout.WEST, txtName);
			springLayout.putConstraint(SpringLayout.SOUTH, txtRepolink, 0, SpringLayout.SOUTH, lblRepositoryLink);
			txtRepolink.setColumns(12);
			contentPane.add(txtRepolink);
		}
		
		
		
		
		{
			txtDescr = new JFormattedTextField();
			if(mode.equals("mod")&&serv.getServDescr()!=null){txtDescr = new JFormattedTextField(serv.getServDescr());}
			else{txtServlink = new JFormattedTextField();}

			springLayout.putConstraint(SpringLayout.NORTH, txtDescr, -5, SpringLayout.NORTH, lblDescr);
			springLayout.putConstraint(SpringLayout.WEST, txtDescr, 0, SpringLayout.WEST, txtName);
			springLayout.putConstraint(SpringLayout.SOUTH, txtDescr, 0, SpringLayout.SOUTH, lblDescr);
			txtDescr.setColumns(12);
			contentPane.add(txtDescr);
		}
		
		{
			txtMeta = new JFormattedTextField();
			
			springLayout.putConstraint(SpringLayout.NORTH, txtMeta, -3, SpringLayout.NORTH, lblMeta);
			springLayout.putConstraint(SpringLayout.WEST, txtMeta, 0, SpringLayout.WEST, txtName);
			springLayout.putConstraint(SpringLayout.SOUTH, txtMeta, 2, SpringLayout.SOUTH, lblMeta);
			txtMeta.setColumns(12);
			contentPane.add(txtMeta);
		}
		
		{
			btnAddMeta = new JButton("Add");
			
			springLayout.putConstraint(SpringLayout.NORTH, btnAddMeta, 0, SpringLayout.NORTH, txtMeta);
			springLayout.putConstraint(SpringLayout.WEST, btnAddMeta, 2, SpringLayout.EAST, txtMeta);
			springLayout.putConstraint(SpringLayout.SOUTH, btnAddMeta, 1, SpringLayout.SOUTH, lblMeta);
			
			btnAddMeta.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					listModel.addElement(txtMeta.getText());
					
				}
			});
			contentPane.add(btnAddMeta);
		}
		
		
		{
			listModel = new DefaultListModel();
			lstMeta = new JList(listModel);
			listSelectionModel = lstMeta.getSelectionModel();
	        listSelectionModel.addListSelectionListener(
	                new SharedListSelectionHandler());
	        listSelectionModel.setSelectionMode(
                    ListSelectionModel.SINGLE_INTERVAL_SELECTION);
	        
	        listPane = new JScrollPane(lstMeta); //scrollbar hooked to the list
	        
	        
			springLayout.putConstraint(SpringLayout.NORTH, listPane, 1, SpringLayout.SOUTH, txtMeta);
			springLayout.putConstraint(SpringLayout.SOUTH, listPane, -43, SpringLayout.SOUTH, contentPane);
			springLayout.putConstraint(SpringLayout.EAST, listPane, 0, SpringLayout.EAST, txtMeta);
			springLayout.putConstraint(SpringLayout.WEST, listPane, 0, SpringLayout.WEST, txtMeta);
			
			listPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			
			
			if (mode.equals("mod")&&serv.getServMeta().size()>0){
				for(int i=0;i<serv.getServMeta().size();i++){
					listModel.addElement(serv.getServMeta().get(i));
				}
			}
			
			contentPane.add(listPane);
		}
		

		{
			btnDelMeta = new JButton("Del");
			
			springLayout.putConstraint(SpringLayout.NORTH, btnDelMeta, 1, SpringLayout.SOUTH, btnAddMeta);
			springLayout.putConstraint(SpringLayout.WEST, btnDelMeta, 2, SpringLayout.EAST, listPane);
			springLayout.putConstraint(SpringLayout.EAST, btnDelMeta, 0, SpringLayout.EAST, btnAddMeta);
			springLayout.putConstraint(SpringLayout.SOUTH, btnDelMeta, 22, SpringLayout.SOUTH, btnAddMeta);
			btnDelMeta.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(selectedMeta>-1)	listModel.remove(selectedMeta);
					
				}
			});
			
			contentPane.add(btnDelMeta);
		}
		
		
		{
			btnAddmod = new JButton("Add");
			if(mode.equals("add"))
			{btnAddmod = new JButton("Add");
			btnAddmod.setToolTipText("Append new service to the bottom of the list");
			}
			else{btnAddmod = new JButton("Modify");
			btnAddmod.setToolTipText("Modify selected service");
			}
			
			btnAddmod.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					
					if (!txtName.getText().trim().equalsIgnoreCase("")||!txtWsdl.getText().trim().equalsIgnoreCase(""))
					
					{
						
						String name,wsdl,swsal=null,bpel=null,servlink=null,repolink=null, description=null;
						
						
						name=txtName.getText();
						wsdl=txtWsdl.getText();
						
						
						if(!txtSwsal.getText().trim().equalsIgnoreCase(""))
						{swsal=txtSwsal.getText();}
						
						if(!txtBpel.getText().trim().equalsIgnoreCase(""))
						{bpel=txtBpel.getText();}
						
						if(!txtServlink.getText().trim().equalsIgnoreCase(""))
						{servlink=txtServlink.getText();}
						
						if(!txtRepolink.getText().trim().equalsIgnoreCase(""))
						{repolink=txtRepolink.getText();}
					
						Link newServ = new Link(name,servlink,wsdl,bpel,swsal,repolink);
						
						
						if(!txtDescr.getText().trim().equalsIgnoreCase(""))
						{ description= txtDescr.getText();
						  newServ.setServDescr(description);
						}
						
						if(listModel.size()>0){
							for(int i=0;i<listModel.size();i++){
								newServ.addServMeta((String)listModel.get(i));
							}
						}
						
						
						try{
						if(mode.equals("add")){
							
							repoview.repomanager.addRepository(newServ, -1);
							
						}else{
							repoview.repomanager.modRepository(newServ, currSel);						
						}
					
						repoview.populateList();
					
						dispose();
						}catch(Exception ex){
							JOptionPane.showMessageDialog(null, "Wrong local file name or incomplete XML","Error", JOptionPane.ERROR_MESSAGE);
						}
					
					}else{JOptionPane.showMessageDialog(null, "Insert Service name or Service wsdl link at least!","Blank fields",2);}
					
				}
			});
			
			springLayout.putConstraint(SpringLayout.WEST, btnAddmod, 0, SpringLayout.WEST, lblName);
			springLayout.putConstraint(SpringLayout.SOUTH, btnAddmod, -8, SpringLayout.SOUTH, contentPane);
			contentPane.add(btnAddmod);
		}

		{
			btnCancel = new JButton("Cancel");
			
			if(!mode.equals("add")){
				springLayout.putConstraint(SpringLayout.WEST, btnCancel, 6, SpringLayout.EAST, btnAddmod);
			}
			springLayout.putConstraint(SpringLayout.SOUTH, btnCancel, 0, SpringLayout.SOUTH, btnAddmod);
			btnCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dispose();
					
				}
			});
			contentPane.add(btnCancel);
		}

		
		{
			btnInsert = new JButton("Insert");
			
			if(mode.equals("add")){
				btnInsert.setVisible(true);
				btnInsert.setToolTipText("Insert new Service before selected service");
				springLayout.putConstraint(SpringLayout.WEST, btnCancel, 6, SpringLayout.EAST, btnInsert);
				springLayout.putConstraint(SpringLayout.WEST, btnInsert, 6, SpringLayout.EAST, btnAddmod);
				springLayout.putConstraint(SpringLayout.SOUTH, btnInsert, 0, SpringLayout.SOUTH, btnAddmod);
				contentPane.add(btnInsert);	
			}else{btnInsert.setVisible(false);}
			
						
			btnInsert.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					if (!txtName.getText().trim().equalsIgnoreCase("")||!txtWsdl.getText().trim().equalsIgnoreCase(""))
						
					{
						
						String name,wsdl,swsal=null,bpel=null,servlink=null,repolink=null;
						name=txtName.getText();
						wsdl=txtWsdl.getText();
						
						
						if(!txtSwsal.getText().trim().equalsIgnoreCase(""))
						{swsal=txtSwsal.getText();}
						
						if(!txtBpel.getText().trim().equalsIgnoreCase(""))
						{bpel=txtBpel.getText();}
						
						if(!txtServlink.getText().trim().equalsIgnoreCase(""))
						{servlink=txtServlink.getText();}
						
						if(!txtRepolink.getText().trim().equalsIgnoreCase(""))
						{repolink=txtRepolink.getText();}
					
						Link newServ = new Link(name,servlink,wsdl,bpel,swsal,repolink);
					
					try{
					if(mode.equals("add")){
						
						repoview.repomanager.addRepository(newServ, currSel);
						
					}
					
					repoview.populateList();
					
					dispose();
					}catch(Exception ex){
						JOptionPane.showMessageDialog(null, "Wrong local file name or incomplete XML","Error", JOptionPane.ERROR_MESSAGE);
					}
					
					}else{JOptionPane.showMessageDialog(null, "Insert Service name or Service wsdl link at least!","Blank fields",2);}
					
				}
			});
			
		}
		
		
		
		
		
	
	
		
}




class SharedListSelectionHandler implements ListSelectionListener {
    public void valueChanged(ListSelectionEvent e) { 
        ListSelectionModel lsm = (ListSelectionModel)e.getSource();

//        int firstIndex = e.getFirstIndex();  	        									 
//        int lastIndex = e.getLastIndex();	 
//        boolean isAdjusting = e.getValueIsAdjusting(); 
      /*  output.append("Event for indexes "
                      + firstIndex + " - " + lastIndex
                      + "; isAdjusting is " + isAdjusting
                      + "; selected indexes:");
*/
        if (lsm.isSelectionEmpty()) {

        	selectedMeta=-1;

        } else {

        	// Find out which indexes are selected.
        	int minIndex = lsm.getMinSelectionIndex();
            int maxIndex = lsm.getMaxSelectionIndex();

            selectedMeta=minIndex;
            
        }
       
      }
}

}

